using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI;
using System.ComponentModel;
using System.Diagnostics;

namespace ARF.Web.Controls
{
    [ToolboxData("<{0}:CannedSiteQuery runat=\"server\" />"), ToolboxItemFilter("ARF")]
    public class CannedSiteQuery : SiteQuery
    {
        private string _listName = "QueryList";
        private string _listWeb = "/";
        private string _queryTitle;

        protected SPListItem _listItem = null;

        public CannedSiteQuery()
        {
            Query = null;
            Webs = null;
            ViewFields = null;
            Lists = null;
            RowLimit = -1;
        }

        public virtual string QueryTitle
        {
            get { return _queryTitle; }
            set { _queryTitle = value; }
        }

        public string QueriesListName
        {
            get { return _listName; }
            set { _listName = value; }
        }

        public string QueriesListWeb
        {
            get { return _listWeb; }
            set { _listWeb = value; }
        }

        protected virtual SPList QueriesList
        {
            get
            {
                try
                {
                    SPWeb oWeb = SPContext.Current.Site.RootWeb;

                    return oWeb.Lists[QueriesListName];
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex);
                    return null;
                }
            }
        }

        protected virtual SPListItem ListItem
        {
            get
            {
                if (_listItem != null) return _listItem;

                SPList oList = QueriesList;

                if (oList == null) return null;

                SPQuery q = new SPQuery();
                q.Query = string.Format("<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq></Where>", QueryTitle);

                SPListItemCollection oResults = oList.GetItems(q);

                if (oResults.Count > 0)
                {
                    _listItem = oResults[0];
                    return _listItem;
                }

                return null;
            }
        }

        public override string ViewFields
        {
            get
            {
                if (base.ViewFields != null) return base.ViewFields;
                
                if (ListItem != null)
                    return (string)ListItem["ViewFields"];

                return "";
            }
            set
            {
                base.ViewFields = value;
            }
        }

        public override string Webs
        {
            get
            {
                if (base.Webs != null) return base.Webs;

                if (ListItem != null)
                    return (string)ListItem["Webs"];

                return "";
            }
            set
            {
                base.Webs = value;
            }
        }

        public override string Lists
        {
            get
            {
                if (base.Lists != null) return base.Lists;

                if (ListItem != null)
                    return (string)ListItem["Lists"];

                return "";
            }
            set
            {
                base.Lists = value;
            }
        }

        public override string Query
        {
            get
            {
                if (base.Query != null) return base.Query;

                if (ListItem != null)
                    return (string)ListItem["Query"];

                return "";
            }
            set
            {
                base.Query = value;
            }
        }

        public override int RowLimit
        {
            get
            {
                if (base.RowLimit != -1) return base.RowLimit;

                if (ListItem != null)
                    return Convert.ToInt32(ListItem["RowLimit"]);

                return 0;
            }
            set
            {
                base.RowLimit = value;
            }
        }
    }
}
